package com.darren.framework.entity;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PostRemove;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.ResultCheckStyle;
import org.hibernate.annotations.SQLDelete;

import com.alibaba.fastjson.annotation.JSONField;
import com.darren.framework.utils.SerializeUtil;


@Entity
@Table(name = "d_login_record")
@SQLDelete(sql = "update d_login_record set is_delete = true where id=?", check = ResultCheckStyle.COUNT)
public class LoginRecord implements Serializable {

	private static final long serialVersionUID = 5810717084282992133L;

	@Id
	@Column(name = "id",columnDefinition = "bigint comment '主键id'")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	/**
	 * 登陆类型：USER 用户登陆,MEMBER 会员登陆
	 */
	@Column(name = "login_type",columnDefinition = "varchar(100) default 'USER' comment '登陆类型：USER 用户登陆,MEMBER 会员登陆'")
	private String loginType;
	
	/**
	 * 登陆者id
	 */
	@Column(name = "login_id",columnDefinition = "bigint comment '登陆者id，用户id或者会员id'")
	private Long loginId;
	
	/**
	 * 登陆时间
	 */
	@JSONField(name="loginTime",format="yyyy-MM-dd HH:mm:ss")
	@Column(name = "login_time",columnDefinition = "datetime default CURRENT_TIMESTAMP comment '登陆时间'")
	private Date loginTime;

	/**
	 * 过期时间，单位：毫秒，默认900000，15分钟
	 */
	@Column(name = "expired_time",columnDefinition = "bigint default 900000 comment '过期时间，单位：毫秒，默认900000，15分钟'")
	private Long expiredTime;

    /**
     * 数据状态，是否删除：true是已删除；false 是未删除
     */
    @Column(name = "is_delete",columnDefinition = "tinyint default 0 comment '数据状态，是否删除：true是已删除；false 是未删除'")
	private boolean delete = false;
    
    
    /**
     * application session
     */
	@Column(name = "app_session",columnDefinition = "longblob default null comment 'application session'")
	private byte[] appSession;

	/**
	 * 登陆session
	 */
	@Transient
	private AppSession session;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getLoginType() {
		return loginType;
	}

	public void setLoginType(String loginType) {
		this.loginType = loginType;
	}

	public Long getLoginId() {
		return loginId;
	}

	public void setLoginId(Long loginId) {
		this.loginId = loginId;
	}

	public Date getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(Date loginTime) {
		this.loginTime = loginTime;
	}

	public Long getExpiredTime() {
		return expiredTime;
	}

	public void setExpiredTime(Long expiredTime) {
		this.expiredTime = expiredTime;
	}

	public boolean isDelete() {
		return delete;
	}

	public void setDelete(boolean delete) {
		this.delete = delete;
	}

	public AppSession getSession() {
		return session;
	}

	public void setSession(AppSession session) {
		this.session = session;
	}

	public byte[] getAppSession() {
		return appSession;
	}

	public void setAppSession(byte[] appSession) throws ClassNotFoundException, IOException {
		if(appSession != null){
			session = (AppSession) SerializeUtil.deserializeObject(appSession);
		}
		this.appSession = appSession;
	}

	public AppSession getApplicationSession() throws ClassNotFoundException, IOException {
		if(session == null && appSession != null){
			session = (AppSession) SerializeUtil.deserializeObject(appSession);
		}
		return session;
	}

	public void setApplicationSession(AppSession session) throws IOException {
		appSession = SerializeUtil.serializeObject(session);
		this.session = session;
	}
	
	@PostRemove
    public void delete() {
        this.delete = true;
    }
	
	 /**
     * 登陆类型
     * @author Darren Zhou
     */
	public static enum LoginType {  
		USER("USER",1),
		MEMBER("MEMBER",2), 
		UNKNOWN("UNKNOWN", -9999); 
		
	    // 成员变量  
	    private String description;  
	    private int code;
	    // 构造方法  
	    private LoginType(String description, int code) {  
	        this.description = description;  
	        this.code = code;  
	    }  
	    // 普通方法  
	    public static String getDescription(int code) {  
	        for (LoginType loginType : LoginType.values()) {  
	            if (loginType.getCode() == code) {  
	                return loginType.description;
	            }  
	        }  
	        return null;  
	    }  
	    // get set 方法  
	    public String getDescription() {  
	        return description;  
	    }  
	    public void setDescription(String description) {  
	        this.description = description;  
	    }  
	    public int getCode() {  
	        return code;  
	    }  
	    public void setCode(int code) {  
	        this.code = code;  
	    }  
	} 	
}
